package com.atguigu.day03;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

/**
 * @author Felix
 * @date 2024/4/1
 * 该案例演示了自定义数据源
 */
public class Flink08_Source_Self {
    public static void main(String[] args) throws Exception {
        //StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(1);
        DataStreamSource<String> dataDS = env.addSource(new MySourceFunction());
        dataDS.print();

        env.execute();
    }
}

class MySourceFunction implements SourceFunction<String>{

    @Override
    public void run(SourceContext<String> ctx) throws Exception {
        //生成(读取)数据的逻辑
        for (int i = 0; i < 100000000 ; i++) {
            //将生成的数据传递到下游
            ctx.collect("数据:" + i);
            Thread.sleep(1000);
        }
    }

    @Override
    public void cancel() {
        //job取消后执行的代码
        System.out.println("~~~Job被取消了~~~");
    }
}
